package com.yimaxiaoerlang.rtc_lib;

import android.util.Log;

import com.yimaxiaoerlang.module_signalling.YMRTMChannel;
import com.yimaxiaoerlang.module_signalling.YMRTMClient;
import com.yimaxiaoerlang.module_signalling.core.YMRTMLoginCallback;
import com.yimaxiaoerlang.module_signalling.core.YMRTMSendPeerMessageCallback;
import com.yimaxiaoerlang.module_signalling.core.YMRtmClientListener;
import com.yimaxiaoerlang.ym_base.YMConfig;

import java.util.Map;

public class YMRLClient implements YMRtmClientListener {
    private static final String TAG = "YMRLClient";
    private static final YMRLClient ourInstance = new YMRLClient();
    private YMRTMClient mClient;
    private YMConnectListener connectListener;
    private YMMessageReceiveListener messageReciveListener;
    private YMCallMessageReciveListener callMessageReciveListener;
    public static String ICE_SERVER_URL = "turn:118.190.89.102:3478";

    public static String getIceServerUrl() {
        return ICE_SERVER_URL;
    }

    public static void setIceServerUrl(String iceServerUrl) {
        ICE_SERVER_URL = iceServerUrl;
    }

    public static YMRLClient getInstance() {
        return ourInstance;
    }

    private YMRLClient() {
    }

    public void initSDKWithConfig(
            YMConfig config
    ) {
        mClient = YMRTMClient.registerAppKey(config.getAppkey(), config.getAppsecret(), this);
    }

    public void initSDK(
            String appKey,
            String appSecret
    ) {
        mClient = YMRTMClient.registerAppKey(appKey, appSecret, this);
    }

    public void setSocketAddress(String address){
        mClient.setSocketAddress(address);
    }

    public void connect(String token, String userId, YMRTMLoginCallback resultCallback) {
        if (mClient != null) {
            mClient.loginWithToken(token, userId, resultCallback);
        }
    }

    public void sendMessage(String peerId, String content) {
        mClient.sendMessage(peerId, content, null);
    }

    public void sendMessage(String peerId, String content, YMRTMSendPeerMessageCallback callback) {
        mClient.sendMessage(peerId, content, callback);
    }

    public YMRTMChannel createChannel(String channelId) {
        return mClient.createChannelById(channelId);
    }
    @Override
    public void onConnectionStateChanged(int state) {
        System.out.println("状态改变$state=" + state);
        if (connectListener != null) {
            connectListener.onChange(state);
        }
    }

    @Override
    public void onMessageReceived(String message, String peerId) {
        Log.e(TAG, "onMessageReceived: " + message + ", "+ peerId );
        if (callMessageReciveListener != null && callMessageReciveListener.onReceive(peerId, message) != true) {
            callMessageReciveListener.onReceive(peerId, message);
        }
    }

    @Override
    public void onTokenExpired() {

    }

    @Override
    public void onPeersOnlineStatusChanged(Map<String, Integer> var1) {

    }

    public void setCallMessageReciveListener(YMCallMessageReciveListener callMessageReciveListener) {
        this.callMessageReciveListener = callMessageReciveListener;
    }


    public void logout() {
        mClient.logoutWithCompletion();
    }

}
